/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.web.backing;

import com.factory.web.provider.HashProvider;
import com.factory.web.dto.UserDTO;
import com.factory.web.service.FactoryServiceInterface;
//import com.factory.web.service.UserServiceInterface;
import com.factory.web.support.FacesUtil;
import javax.faces.application.FacesMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

/**
 *
 * @author milos
 */
@Component("registrationBackingBean")
@Scope("request")
public class RegistrationBackingBean {

    private UserDTO userDTO;
    private String password;
    @Autowired
    private FactoryServiceInterface factoryService;
    @Autowired
    private HashProvider hashProvider;

    public RegistrationBackingBean() {
        this.userDTO = new UserDTO();
    }

    public String saveUser() {
        
        userDTO.setPassHash(hashProvider.computeHash(password));
        userDTO.setBlocked(false);
        if (!factoryService.register(userDTO)) { //this user is blocked
            FacesUtil.addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Forbidden", "User is blocked."));
            return "registration";
        } else {
            FacesUtil.addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Succesful", "User created."));
            return "index";
        }
    }

    public UserDTO getUserDTO() {
        return userDTO;
    }

    public void setUserDTO(UserDTO userDTO) {
        this.userDTO = userDTO;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
